package com.mybatis.dao;

import com.mybatis.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {

    @Insert("insert into user values ( #{id},#{username},#{password},#{date} )")
    void save(User user);

    @Update("update user set username=#{username},password=#{password} where id=#{id}")
    void update(User user);

    @Delete("delete from user where id=#{id}")
    void delete(int id);

    @Select("select * from user where id=#{id}")
    User findById(int id);

    @Select("select * from user")
    List<User> findAll();

    @Select("select * from user")
    @Results({
            //标识为id
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(
                    property = "orders",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.mybatis.dao.OrderDao.findById") //一对多注解
            )
    })
    List<User> findUserByOrdersAll();


    @Select("select * from user")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(
                    property = "roles",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.mybatis.dao.RoleDao.findByUid")
            )
    })
    List<User> findUserByRoleAll();

}
